import { processEl} from "./auth";

export function initRules(thisObj) {
  // 开始初始化所有项目为必填
  const initRuleArr = [{ required: true }]
  const formKey = thisObj.initUiVal[thisObj.initFormKey].formKey
  for (const idx in formKey) {
    const key = formKey[idx].key
    thisObj.rules[key] = Object.assign([], initRuleArr)
  }
}

export function changeFormRules(thisObj) {
  // 加载结束后,根据规则调整对应表单校验方式
  const formKey = thisObj.initUiVal[thisObj.initFormKey].formKey
  for (const idx in formKey) {
    const key = formKey[idx].key
    thisObj.changeFormKeyRules(key)
  }
}

export function initLazyFormRules(thisObj) {
  // 单个变量校验规则调整设置
  const rules = thisObj.initUiVal[thisObj.initFormKey].rules
  if (rules !== undefined && rules !== null) {
    const formKey = thisObj.initUiVal[thisObj.initFormKey].formKey
    for (const idx in formKey) {
      const key = formKey[idx].key
      let newRules = []
      newRules = getObjElRules(thisObj, key, rules)
      if (newRules.length === 0) {
        const notRequired = {required: false}
        newRules.push(notRequired)
      }
      thisObj.lazyFormRules[key] = Object.assign([])
      const tmpData = {}
      tmpData[key] = newRules
      const cccc = Object.assign({}, thisObj.lazyFormRules, tmpData)
      // 只有这样，对象才发生变化,子组件才能监听到数据
      thisObj.lazyFormRules = Object.assign({}, cccc)
    }
  }
}

export function changeFormKeyRules(thisObj, key) {
  // 单个变量校验规则调整设置
  let newRules = []
  if (thisObj.initUiVal[thisObj.initFormKey].rules !== null) {
    newRules = getObjElRules(thisObj, key, thisObj.initUiVal[thisObj.initFormKey].rules)
    if (newRules.length === 0) {
      const notRequired = { required: false }
      newRules.push(notRequired)
    }
    thisObj.rules[key] = Object.assign([])
  }
  const tmpData = {}
  tmpData[key] = newRules
  const cccc = Object.assign({}, thisObj.rules, tmpData)
  // 只有这样，对象才发生变化,子组件才能监听到数据
  thisObj.rules = Object.assign({}, cccc)
}

/**
 * 重组表单为非必填校验
 * @param thisObj 当前对象
 * @param 变量
 * @returns {any}
 */
export function resetNotRequiredRuleObj(thisObj, key) {
  const tmpObj = Object.assign({})
  const newRules = Object.assign([])
  const notRequired = { required: false }
  newRules.push(notRequired)
  tmpObj[key] = newRules
  const cccc = Object.assign({}, thisObj.rules, tmpObj)
  // 只有这样，对象才发生变化,子组件才能监听到数据
  thisObj.rules = Object.assign({}, cccc)
  return tmpObj
}

export function clearFormRuleObj(thisObj, key) {
  const tmpObj = Object.assign({})
  const newRules = Object.assign([])
  const notRequired = { required: false }
  newRules.push(notRequired)
  tmpObj[key] = newRules
  const cccc = Object.assign({}, thisObj.rules, tmpObj)
  // 只有这样，对象才发生变化,子组件才能监听到数据
  thisObj.rules = Object.assign({}, cccc)
  return tmpObj
}
export function getObjElRules(thisObj, key, ruleObj) {
  const newRules = Object.assign([])
  if (undefined !== ruleObj[key]) {
    const rules = ruleObj[key]
    for (const idx in rules) {
      const ruleData = rules[idx]
      const tmpData = Object.assign({}, ruleData)
      // if (undefined === ruleData.el) {
      if (undefined === tmpData.objEl) {
        newRules.push(tmpData)
      } else {
        const elFlag = processEl(thisObj.globalData.formModel, tmpData.objEl)
        if (elFlag) {
          newRules.push(tmpData)
        }
      }
    }
  }
  return newRules
}
